package com.icore.ui;

import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.message.BasicNameValuePair;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import com.icore.R;
import com.icore.common.LogUtils;
import com.icore.common.UIHelper;
import com.icore.ui.base.BaseActivity;

/**
 * 主页面
 * 
 */
public class MainActivity extends BaseActivity {

	private String roldID = "";
	private String userID = "";

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_view);

		// 获取传递过来的数据
		getIntentEntra();

		GridView gridview = (GridView) findViewById(R.id.gridview);

		// 生成动态数组，并且转入数据
		ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
		initNineData(this, lstImageItem);

		// 生成适配器的ImageItem <====> 动态数组的元素，两者一一对应
		SimpleAdapter saImageItems = new SimpleAdapter(this, // 没什么解释
				lstImageItem,// 数据来源
				R.layout.night_item,// night_item的XML实现
				new String[] { "ItemImage", "ItemText" },// 动态数组与ImageItem对应的子项
				new int[] { R.id.ItemImage, R.id.ItemText }); // ImageItem的XML文件里面的一个ImageView, 两个TextView ID
		// 添加并且显示
		gridview.setAdapter(saImageItems);
		// 添加消息处理
		gridview.setOnItemClickListener(new ItemClickListener());
	}

	private void getIntentEntra() {
		Intent intent = getIntent();
		roldID = intent.getStringExtra("rold_id");
		userID = intent.getStringExtra("user_id");
		LogUtils.printOutToConsole(roldID + ", " + userID);
	}

	private void initNineData(MainActivity activity, ArrayList<HashMap<String, Object>> lstImageItem) {
		int itemImage[] = { R.drawable.nine_daiban, R.drawable.nine_gouche, R.drawable.nine_weixiu, R.drawable.nine_renwu,
				R.drawable.nine_jixiao };
		String itemText[] = { activity.getString(R.string.daiban), activity.getString(R.string.gouche),
				activity.getString(R.string.weixiu), activity.getString(R.string.renwu), activity.getString(R.string.jixiao) };

		for (int i = 0; i < itemImage.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("ItemImage", itemImage[i]);
			map.put("ItemText", itemText[i]);
			lstImageItem.add(map);
		}

	}

	// 当AdapterView被单击(触摸屏或者键盘)，则返回的Item单击事件
	class ItemClickListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			switch (position) {
			// 待办事项
			case 0:
				break;
			// 购车
			case 1:
				UIHelper.startActivity(MainActivity.this, GoucheListActivity.class, new BasicNameValuePair("user_id", userID));
				break;
			// 维修
			case 2:

				break;
			// 任务
			case 3:

				break;
			// 绩效跟踪
			case 4:

				break;
			}
		}

	}

}
